/*
 * @features: 功能
 * @description: 说明
 * @Date: 2019-08-06 12:27:45
 * @Author: judu233(769471424@qq.com)
 * @LastEditTime: 2020-08-17 23:11:37
 * @LastEditors: judu233
 */
import arrow from "./arrow";

const { ccclass, property } = cc._decorator;

@ccclass
export default class arrow_scene extends cc.Component {
    @property(arrow)
    curArrow: arrow = null;
    @property(cc.Node)
    target: cc.Node = null;

    onLoad() {
        this.node.on(cc.Node.EventType.TOUCH_MOVE, this.controlArrow, this);
        this.node.on(cc.Node.EventType.TOUCH_END, this.hideArrow, this);
        this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.hideArrow, this);
    }

    /**控制箭头 */
    private controlArrow(e): void {
        var location = e.getLocation();
        this.checkIsOnTarget(location);
        var ePos = this.curArrow.node.convertToNodeSpaceAR(location);
        this.curArrow.controlArrows(cc.v2(), ePos);
        this.curArrow.node.active = true;
    }

    /**隐藏箭头 */
    private hideArrow(e): void {
        this.curArrow.node.active = false;
        this.curArrow.controlArrows(cc.v2(), cc.v2());
    }

    //判断主箭头是否移到目标target
    private checkIsOnTarget(pos: cc.Vec2): void {
        //rect是以target的父节点为基准的，那point也需要以target的父节点为基准
        var rect = this.target.getBoundingBox();
        var point = this.target.parent.convertToNodeSpaceAR(pos);
        this.curArrow.newOnTargetStatus = rect.contains(point);
    }

}
